//
//  LJFSearchViewController.m
//  GiftThat
//
//  Created by 刘景峰 on 16/1/8.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "LJFSearchViewController.h"
#import "HotTopicsViewController.h"
#import "LJFNewSearchViewController.h"
#import "LJFChooseGiftViewController.h"
@interface LJFSearchViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UISearchBar *searchBar;
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation LJFSearchViewController

-(void)viewWillAppear:(BOOL)animated{
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Color"] isEqualToString:@"BlackColor"]) {
        self.tableView.backgroundColor = [UIColor colorWithRed:35 / 255.0 green:34 / 255.0 blue:38 / 255.0 alpha:1.0];
        [self.tableView reloadData];
    }else{
        self.tableView.backgroundColor =  [UIColor whiteColor];
        [self.tableView reloadData];
    }
 
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:234 / 255.0 green:234 / 255.0 blue:241 / 255.0 alpha:1];
//    self.view.backgroundColor = [UIColor whiteColor];
    
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.width - 100, 44)];
    
    self.searchBar.delegate = self;
//显示searchbar的右侧取消按钮
    self.searchBar.showsCancelButton = YES;
    self.navigationItem.titleView = self.searchBar;
//取消navigation的返回按钮
    self.navigationItem.hidesBackButton = YES;
//    searchbar成为第一响应者
    [self.searchBar becomeFirstResponder];
    self.searchBar.keyboardType = UIKeyboardTypeNamePhonePad;
    
//    网络请求
    [self getURL];
//    创建tableview
    [self creatTableView];
    
    
}

#pragma mark-网络请求

-(void)getURL{
    [CXNetWorkTool getWithUrl:@"http://api.liwushuo.com/v2/search/hot_words" parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dic = [[NSDictionary alloc]initWithDictionary:[result objectForKey:@"data"]];
        self.searchBar.placeholder = [dic objectForKey:@"placeholder"];
        [self.tableView reloadData];
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}


#pragma mark-搜索按钮和取消按钮的事件
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
//    取消键盘的第一响应收起键盘
    [self.searchBar resignFirstResponder];
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSString *string = @"http://api.liwushuo.com/v2/search/item?keyword=%@&limit=20&offset=0&sort=";
    NSString *newString1 = [self.searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *nextString = [NSString stringWithFormat:string,newString1];
    LJFNewSearchViewController *new = [[LJFNewSearchViewController alloc]init];
    [self.searchBar resignFirstResponder];
    new.urlString = nextString;
    new.palceHolder = self.searchBar.text;
    [self.navigationController pushViewController:new animated:YES];

}
#pragma mark-collectionview的创建和方法
//collectionview的创建
-(void)creatTableView{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.backgroundColor = [UIColor blackColor];
    
    [self.tableView registerClass:[LJFSearchTableViewCell class] forCellReuseIdentifier:@"LJFSearchTableViewCell"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ICollectionViewCell"];
    [self.view addSubview:self.tableView];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
    LJFSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LJFSearchTableViewCell"];
        cell.delegate = self;
        return cell;

    }else{
        UITableViewCell *new = [tableView dequeueReusableCellWithIdentifier:@"ICollectionViewCell"];
        new.textLabel.text = @"使用选礼神器快速挑选礼物";
        [new.textLabel  setFont:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:13]];
//        添加cell的向右的箭头
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Color"] isEqualToString:@"BlackColor"]) {
            new.backgroundColor = [UIColor colorWithRed:35 / 255.0 green:34 / 255.0 blue:38 / 255.0 alpha:1.0];
            new.textLabel.textColor = [UIColor whiteColor];
                    }else{
                        new.backgroundColor = [UIColor whiteColor];
                        new.textLabel.textColor =[UIColor blackColor];
        }

        
        
        new.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        new.imageView.image = [UIImage imageNamed:@"dingwei"];
        return new;
    
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 150;
    }else{
        return 44;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 20;
    }else{
        return 10;
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
 
    if (section == 0) {
        
        return @"大家都在搜";
    }else{
        return nil;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        LJFChooseGiftViewController *choose = [[LJFChooseGiftViewController alloc]init];
        [self.searchBar resignFirstResponder];
        [self.navigationController pushViewController:choose animated:YES];
    }
    
}


#pragma mark-协议方法的实现

-(void)jumpToYourSearch:(NSString *)url Withstring:(NSString *)string{
    LJFNewSearchViewController *newSearch = [[LJFNewSearchViewController alloc]init];
    newSearch.urlString = url;
    newSearch.palceHolder = string;
    [self.searchBar resignFirstResponder];
    [self.navigationController pushViewController:newSearch animated:YES];
}

#pragma mark- collectionview遗留问题自己看看
//-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
